$(document).ready(function(){
	//默认的第一次读取
	loadSpecial();
	
	var chineseNum = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
	
	//读取完毕即可触发小写转大写
	setCapitalTextOfRMB($('[name=applyInfo.realCost]'), 's');
	setCapitalTextOfRMB($('[name=applyInfo.bargainCost]'), 'b');
	setCapitalTextOfRMB($('[name=applyInfo.applyRepay]'), 'a');
	
	//每次改变时读取
	$('[name=applyInfo.departmentId]').change(function(){
		loadSpecial();
	});
	
	//绑定失去焦点函数触发小写转大写
	$('[name=applyInfo.realCost]').blur(function(){
		if(!isNumber($('[name=applyInfo.realCost]').val())){
			alert("请输入数字");
		}
		setCapitalTextOfRMB($('[name=applyInfo.realCost]'), 's');
	});
	
	$('[name=applyInfo.bargainCost]').blur(function(){
		if(!isNumber($('[name=applyInfo.bargainCost]').val())){
			alert("请输入数字");
		}
		setCapitalTextOfRMB($('[name=applyInfo.bargainCost]'), 'b');
	});
	
	$('[name=applyInfo.applyRepay]').blur(function(){
		if(!isNumber($('[name=applyInfo.applyRepay]').val())){
			alert("请输入数字");
		}
		setCapitalTextOfRMB($('[name=applyInfo.applyRepay]'), 'a');
	});
	
	var alertMsg;
	//提交事件的处理
	$('#submit').click(function(event){
		alertMsg = "";
		//此处应加验证
		var isFailed = validateForm();
		if(isFailed){
			alert(alertMsg);
			event.preventDefault();
			return;
		}
		var con = confirm("确定提交？");
		if (!con) {
			event.preventDefault();
		}
		else {
			alert("提交成功！");
		}
	});
	
	//根据departmentId从后台获取专业并显示到前台select标签中
	function loadSpecial(){
		var departmentId = $('[name=applyInfo.departmentId]').val();
		$.post("dropList.action",{departmentId:departmentId},function(data){
			var json = eval(data);
			var len = json.length;
			var selectedVal = $('#specialId').val();
			$('[name=applyInfo.specialId]').empty();
			for(var i=0; i<len; i++){
				if(json[i].id == selectedVal){
					$('[name=applyInfo.specialId]').append('<option value="' + json[i].id + '" selected="selected">' + json[i].specialName + '</option>');
				}else{
					$('[name=applyInfo.specialId]').append('<option value="' + json[i].id + '">' + json[i].specialName + '</option>');
				}
			}
		});
	}
	
	//将小写转大写
	function setCapitalTextOfRMB(obj, prefix){
		var sVal = obj.val();
		if(sVal == ""){
			$('#' + prefix + '_rmbW').val("");
			$('#' + prefix + '_rmbQ').val("");
			$('#' + prefix + '_rmbB').val("");
			$('#' + prefix + '_rmbS').val("");
			$('#' + prefix + '_rmbY').val("");
			return;
		}
		var sW = parseInt(sVal / 10000);
		var sQ = parseInt(sVal % 10000 / 1000);
		var sB = parseInt(sVal % 1000 / 100);
		var sS = parseInt(sVal % 100 / 10);
		var sY = parseInt(sVal % 10);
		$('#' + prefix + '_rmbW').val(chineseNum[sW]);
		$('#' + prefix + '_rmbQ').val(chineseNum[sQ]);
		$('#' + prefix + '_rmbB').val(chineseNum[sB]);
		$('#' + prefix + '_rmbS').val(chineseNum[sS]);
		$('#' + prefix + '_rmbY').val(chineseNum[sY]);
	}
	
	function validateForm(){
		var hasError = false;
		
		var storeValue;
		
		storeValue = $('[name=applyInfo.name]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写姓名\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 姓名填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.politicsStatus]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写政治面貌\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 政治填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.birthday]').val();
		
		if (isNull(storeValue)) {
			alertMsg += "-- 未选择出生日期\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.graduationSchool]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写毕业学校\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 毕业学校填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.graduationDate]').val();
		
		if (isNull(storeValue)) {
			alertMsg += "-- 未选择毕业日期\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.serveYear]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写与就业单位签订的服务年限\n";
			hasError = true;
		}else if(!isNumber(storeValue)){
			alertMsg += "-- 与就业单位签订的服务年限填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.bankCard]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写交行卡号\n";
			hasError = true;
		}else if(!isNumber(storeValue)){
			alertMsg += "-- 交行卡号填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.telPhone]').val();
		
		if(isNull(storeValue)){
		}else if(isPhoneNumber(storeValue)){
			alertMsg += "-- 本人固定电话填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.mobilePhone]').val();
		
		if(isNull(storeValue)){
		}else if(!isPhoneNumber(storeValue)){
			alertMsg += "-- 本人移动电话填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.QQ]').val();
		
		if(isNull(storeValue)){
		}else if(!isNumber(storeValue)){
			alertMsg += "-- QQ号码填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.email]').val();
		
		if(isNull(storeValue)){
		}else if(isEmail(storeValue)){
			alertMsg += "-- email填写有误，请按照正确格式填写\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.homeAddress]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写家庭地址\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 家庭地址填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.homePostCode]').val();
		
		if(isNull(storeValue)){
		}
		else if((toreValue.length > 6) ||storeValue(!isNumber(storeValue))){
			alertMsg += "-- 家庭所在地邮编填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.homePhone]').val();
		
		if(isNull(storeValue)){
		}else if(!isNumber(storeValue)){
			alertMsg += "-- 家庭固定电话填写有误，请填写数字\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.workPlaceName]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写就业单位名称\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 就业单位名称填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.workPlacePhone]').val();
		
		if(isNull(storeValue)){
			alertMsg += "-- 未填写就业单位地址\n";
			hasError = true;
		}else if(isNumber(storeValue)){
			alertMsg += "-- 就业单位地址填写有误\n";
			hasError = true;
		}
		
		storeValue = $('[name=applyInfo.workPlacePostCode]').val();
		
		if(isNull(storeValue)){
		}
		else if((toreValue.length > 6) ||storeValue(!isNumber(storeValue))){
			alertMsg += "-- 就业单位所在地邮编填写有误，请填写数字\n";
			hasError = true;
		}
		
		if(hasError){
			alertMsg += "请更正以上错误";
		}
		return hasError;
	}
	
	//测试给定值是否为空
	function isNull(data){
		if(data == ""){
			return true;
		}else{
			return false;
		}
	}
	
	//测试给定值是否为数字
	function isNumber(data){
		return !isNaN(data);
	}
	
	function isPhoneNumber(data){
		var myRegExp = /[0-9]*-?[0-9]+/
		return myRegExp.test(data);
	}
	
	//测试给定的Email地址是否合法
	function isEmail(data){
    	var myRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/ ;
   		return myRegExp.test(data)
	}
})